﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using BusinessObjects;
using System.Linq;

namespace UI
{
    public partial class frmThietLapChoNam_SVGiangVien : FormBase
    {
        public frmThietLapChoNam_SVGiangVien()
        {
            InitializeComponent();
        }

        public int NamThucTapID { get; set; }
        public int GiangVienID { get; set; }
        private DataTable _dtSinhVienChon;
        private DataTable _dtSinhVien;

        private void frmThietLapChoNam_SVGiangVien_Load(object sender, EventArgs e)
        {
            try
            {
                _dtSinhVienChon = new DataTable();
                _dtSinhVienChon.Columns.Add("Choose", typeof(bool));
                _dtSinhVienChon.Columns.Add("SinhVienID", typeof(string));
                _dtSinhVienChon.Columns.Add("MaSinhVien", typeof(string));
                _dtSinhVienChon.Columns.Add("Ho", typeof(string));
                _dtSinhVienChon.Columns.Add("Ten", typeof(string));
                _dtSinhVienChon.Columns.Add("NganhHoc", typeof(string));
                _dtSinhVienChon.Columns.Add("LopHoc", typeof(string));
               // _dtCurrent.Columns.Add("Email", typeof(string));
               
                _dtSinhVien = new DataTable();
                _dtSinhVien.Columns.Add("Choose", typeof(bool));
                _dtSinhVien.Columns.Add("SinhVienID", typeof(string));
                _dtSinhVien.Columns.Add("MaSinhVien", typeof(string));
                _dtSinhVien.Columns.Add("Ho", typeof(string));
                _dtSinhVien.Columns.Add("Ten", typeof(string));
                _dtSinhVien.Columns.Add("NganhHoc", typeof(string));
                _dtSinhVien.Columns.Add("LopHoc", typeof(string));
               // _dtChoose.Columns.Add("Email", typeof(string));
                
                LoadSinhVien();
                LoadSinhVienChon();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                DataRow[] drs = _dtSinhVien.Select("Choose=1");
                if (drs.Length > 0)
                {
                    if (CommonConfirm("Bạn muốn thêm dữ liệu này?", "LUU"))
                    {
                        using (NestedUnitOfWork nuow = Session.DefaultSession.BeginNestedUnitOfWork())
                        {
                            GiangVienTrongNam objGiangVien = nuow.Query<GiangVienTrongNam>().FirstOrDefault(c => c.Oid == GiangVienID);
                            if (objGiangVien != null)
                            {
                                for (int i = 0; i < drs.Length; i++)
                                {
                                    DataRow dr = drs[i];
                                    int sinhvienID = Convert.ToInt32(dr["SinhVienID"]);
                                    var objSinhVien = nuow.Query<SinhVien>().FirstOrDefault(c => c.Oid == sinhvienID);
                                    if (objSinhVien != null)
                                    {
                                        SinhVienTrongGiangVien obj = new SinhVienTrongGiangVien(nuow);
                                        obj.GiangVienTrongNam = objGiangVien;
                                        obj.SinhVien = objSinhVien;
                                        obj.Diem = 0;
                                        obj.NhanXet = string.Empty;
                                    }
                                }
                                nuow.CommitChanges();
                            }
                        }
                        //CommonMessage("Cập nhật thành công");
                        LoadSinhVien();
                        LoadSinhVienChon();
                    }
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                DataRow[] drs = _dtSinhVienChon.Select("Choose=1");
                if (drs.Length > 0)
                {
                    if (CommonConfirm("Bạn muốn xóa dữ liệu này?", "LUU"))
                    {
                        using (NestedUnitOfWork nuow = Session.DefaultSession.BeginNestedUnitOfWork())
                        {                            
                            NamThucTap objNamThucTap = nuow.Query<NamThucTap>().FirstOrDefault(c => c.Oid == NamThucTapID);
                            if (objNamThucTap != null)
                            {
                                for (int i = 0; i < drs.Length; i++)
                                {
                                    DataRow dr = drs[i];
                                    int sinhvienID = Convert.ToInt32(dr["SinhVienID"]);
                                    var obj = nuow.Query<SinhVienTrongGiangVien>().FirstOrDefault(c => c.Oid == sinhvienID);
                                    if (obj != null)
                                    {
                                        nuow.Delete(obj);
                                    }
                                }
                                nuow.CommitChanges();
                            }
                        }
                        CommonMessage("Cập nhật thành công");
                        LoadSinhVien();
                        LoadSinhVienChon();
                    }
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void LoadSinhVien()
        {
            var query = Session.DefaultSession.Query<SinhVienTrongGiangVien>().Where(c => c.GiangVienTrongNam.Oid == GiangVienID).Select(c => new
            {
                c.Oid,
                c.SinhVien.MaSinhVien,
                c.SinhVien.HoSinhVien,
                c.SinhVien.TenSinhVien,
                c.SinhVien.LopHoc,
               // c.SinhVien.Email,
                c.SinhVien.NganhHoc.TenNganhHoc
            });
            foreach (var item in query)
            {
                DataRow dr = _dtSinhVienChon.NewRow();
                dr["Choose"] = false;
                dr["SinhVienID"] = item.Oid;
                dr["MaSinhVien"] = item.MaSinhVien;
                dr["Ho"] = item.HoSinhVien;
                dr["Ten"] = item.TenSinhVien;
                dr["NganhHoc"] = item.TenNganhHoc;
                dr["LopHoc"] = item.LopHoc;
               // dr["Email"] = item.Email;
                _dtSinhVienChon.Rows.Add(dr);
            }
            gcSinhVienChon.DataSource = _dtSinhVienChon;
        }

        private void LoadSinhVienChon()
        {
            var query = Session.DefaultSession.Query<SinhVien>().Where(c => c.SinhVienTrongNams.Any(d=>d.NamThucTap.Oid==NamThucTapID) && !c.SinhVienTrongGiangViens.Any(d=>d.GiangVienTrongNam.NamThucTap.Oid == NamThucTapID)).Select(c => new
            {
                c.Oid,
                c.MaSinhVien,
                c.HoSinhVien,
                c.TenSinhVien,
                c.LopHoc,
             //   c.Email,
                c.NganhHoc.TenNganhHoc
            });
            foreach (var item in query)
            {
                DataRow dr = _dtSinhVien.NewRow();
                dr["Choose"] = false;
                dr["SinhVienID"] = item.Oid;
                dr["MaSinhVien"] = item.MaSinhVien;
                dr["Ho"] = item.HoSinhVien;
                dr["Ten"] = item.TenSinhVien;
                dr["NganhHoc"] = item.TenNganhHoc;
                dr["LopHoc"] = item.LopHoc;
              //  dr["Email"] = item.Email;
                _dtSinhVien.Rows.Add(dr);
            }
            gcSinhVien.DataSource = _dtSinhVien;
        }

        private void ckbChonTatCa_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                foreach (DataRow dr in _dtSinhVien.Rows)
                    dr["Choose"] = Convert.ToBoolean(ckbChonTatCa.EditValue);
                
                gcSinhVien.DataSource = _dtSinhVien;
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }
    }
}